package com.ws.hzuoj.admin.system.genetration.web;

import cn.hutool.core.util.StrUtil;
import com.ws.common.easyui.util.Page;
import com.ws.common.easyui.util.PageUtils;
import com.ws.hzuoj.admin.system.genetration.entity.GenerationConfig;
import com.ws.hzuoj.admin.system.genetration.service.IGenerationConfigService;
import com.ws.hzuoj.admin.system.genetration.util.generationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RequestMapping("/admin/system/generation/config")
@Controller
public class GenerationConfigWeb {


    @Autowired
    IGenerationConfigService generationConfigService;

    @RequestMapping("{type}")
    public String list(@PathVariable String type){
        return  null;
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(GenerationConfig generationConfig,Page page){
        List<GenerationConfig> generationConfigs=generationConfigService.list(generationConfig,page);
        for(GenerationConfig config: generationConfigs){
            if(StrUtil.isEmpty(config.getId())){
                generationUtils.setDefault(config);
                generationConfigService.insert(config);
            }
        }
        return PageUtils.getEasyUiList(generationConfigs);
     }

    @RequestMapping("read")
    @ResponseBody
    public Object read(GenerationConfig generationConfig){
        if(StrUtil.isNotEmpty(generationConfig.getId())){
            generationConfig= generationConfigService.read(generationConfig.getId());
        }
        return  generationConfig;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(GenerationConfig generationConfig){
        Integer count=generationConfigService.save(generationConfig);
        return  count;
     }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(String id){
        return  generationConfigService.delete(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(GenerationConfig generationConfig){
        Integer count=generationConfigService.insert(generationConfig);
        return  count;
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public Object insertSelective(GenerationConfig generationConfig){
        return generationConfigService.insertSelective(generationConfig);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(GenerationConfig generationConfig){
        return generationConfigService.update(generationConfig);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listByMap(){
        return  generationConfigService.listAll();
    }

    @RequestMapping("/generation")
    @ResponseBody
    public Object generation(GenerationConfig generationConfig, HttpServletRequest request, HttpServletResponse httpServletResponse){
        generationConfig= generationConfigService.read(generationConfig.getId());
        RestTemplate template=new RestTemplate();;



        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("configJson",generationConfig);
        HttpHeaders headers = new HttpHeaders();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            headers.add(key, value);
        }
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.setContentType(null);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> response=template.postForEntity("http://localhost:8082/generation/adminSc",requestEntity,String.class);
        httpServletResponse.setStatus(response.getStatusCodeValue());
        return "生成成功！";
    }
}
